angular.module('starter.controllers', ['service'])

.controller('AppCtrl', function($scope, $ionicModal, $timeout,$state,LoginService,ls) {

  // With the new view caching in Ionic, Controllers are only called
  // when they are recreated or on app start, instead of every page change.
  // To listen for when this page is active (for example, to refresh data),
  // listen for the $ionicView.enter event:
  //$scope.$on('$ionicView.enter', function(e) {
  //});

  // Form data for the login modal
  $scope.loginData = {};

  // Create the login modal that we will use later
  $ionicModal.fromTemplateUrl('templates/login.html', {
    scope: $scope
  }).then(function(modal) {
    $scope.modal = modal;
  });

  // Triggered in the login modal to close it
  $scope.closeLogin = function() {
    $scope.modal.hide();
  };

  // Open the login modal
  $scope.login = function() {
    $scope.modal.show();
  };

  // Perform the login action when the user submits the login form
  $scope.doLogin = function() {
    console.log('Doing login', $scope.loginData);
    LoginService.getData($scope.loginData).success(function(response) {
            console.log(response)
            var msg;
            if (response.length==32) {
              ls.set('username',$scope.loginData.username)
              ls.set("token",response);
              msg="登陆成功";
              alert(msg)
              $scope.closeLogin();
              $state.go('app.playlists')
            }else{
              switch (response)
              {
              case 'error':
                msg="用户不存在或者密码错误！";
                alert(msg);
                break;
              default:
                msg="服务器出错，请联系管理员";
                alert(msg);
              }
            }
              
            
            
          }).error(function(data) {
              alert("网络出错！")
          });

  };
})

.controller('PlaylistsCtrl', function($scope,WorkflowService) {
  WorkflowService.getData($scope.loginData).success(function(response) {
            console.log(response)
            $scope.playlists = response.data
  
          }).error(function(data) {
              alert("网络出错！")
          });
  
})
.controller('PlaylistCtrl', function($scope, $stateParams,$state,SearchService,ApproveService) {
  if ($stateParams.process=="租赁合同审批-工作流") {
    SearchService.getData($stateParams.id).success(function(response) {
            console.log(response)
             $scope.items=response;

          }).error(function(data) {
              alert("网络出错！")
          });
    $scope.approve=function(arg){
        var reply=(arg==-1?'':'pass');
        var data={'id':$scope.items.pkContract,'reply':reply,'answer':'','isto':0}
        ApproveService.approve(data).success(function(response){
          switch (response)
              {
              case 'NoPermission':
                alert('当前用户没有审批权限！');
                break;
              case 'no':
                alert('数据不存在或已被删除!');
                break;
              case 'ok':
                alert('审批成功!');
                break;
              default:
                msg="服务器出错，请联系管理员";
                alert(msg);
              }
          $state.go('app.playlists');
          
        })
    }
  };
    
})
